#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QGridLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QPushButton *but1 = new QPushButton("按钮1");
    QPushButton *but2 = new QPushButton("按钮2");
    QPushButton *but3 = new QPushButton("按钮3");
    QPushButton *but4 = new QPushButton("按钮4");
    QPushButton *but5 = new QPushButton("按钮5");
    QPushButton *but6 = new QPushButton("按钮6");

    QGridLayout *out = new QGridLayout();
    out->addWidget(but1,0,0);
    out->addWidget(but2,0,1);
    out->addWidget(but3,1,0);
    out->addWidget(but4,1,1);
    out->addWidget(but5,2,0);
    out->addWidget(but6,2,1);
    this->setLayout(out);

    // 要设置垂直拉伸系数必须设置按钮的 sizePolicy, 此时按钮的水平方向和垂直方向都会尽量舒展开
    but1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    but2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    but3->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    but4->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    but5->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    but6->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    // 设置垂直拉伸系数
    out->setRowStretch(0,1);
    out->setRowStretch(1,1);
    out->setRowStretch(2,3);
    // 设置水平拉伸系数
    out->setColumnStretch(0,1);
    out->setColumnStretch(1,2);
}

Widget::~Widget()
{
    delete ui;
}
